# -*- coding: utf-8 -*-

import os, glob
import csv
import random

class_to_num = {}

class_name_list = os.listdir('animals')
print(class_name_list) # ['cat', 'dog']

for class_name in class_name_list:
    class_to_num[class_name] = len(class_to_num.keys())
    
print(class_to_num) #{'cat': 0, 'dog': 1}

image_dir = []
for class_name in class_name_list:
    image_dir += glob.glob(os.path.join('animals', class_name, '*.jpg'))
    
print(image_dir) # ['animals\\cat\\1.jpg', 'animals\\cat\\10.jpg', 'animals\\cat\\2.jpg', 'animals\\cat\\3.jpg', 'animals\\cat\\4.jpg', 'animals\\cat\\5.jpg', 'animals\\cat\\6.jpg', 'animals\\cat\\7.jpg', 'animals\\cat\\8.jpg', 'animals\\cat\\9.jpg', 'animals\\dog\\1.jpg', 'animals\\dog\\10.jpg', 'animals\\dog\\2.jpg', 'animals\\dog\\3.jpg', 'animals\\dog\\4.jpg', 'animals\\dog\\5.jpg', 'animals\\dog\\6.jpg', 'animals\\dog\\7.jpg', 'animals\\dog\\8.jpg', 'animals\\dog\\9.jpg']

random.shuffle(image_dir) # 打乱顺序
with open('myself_data.csv',  mode='w', newline='') as f:
    writer = csv.writer(f)
    for image in image_dir:
        class_name = image.split(os.sep)[-2]
        label = class_to_num[class_name]
        writer.writerow([image, label])